package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.QuickAdapterHelper;
import com.student.dms.databinding.FragmentAdminHomeBinding;
import com.student.dms.databinding.FragmentAdminManagerBinding;
import com.student.dms.databinding.ItemRecyerviewBinding;

import java.util.ArrayList;

public class AdminHomeFragment extends BaseFragment<FragmentAdminHomeBinding>  {

    @Override
    public void setViewBinding() {
        mViewBinding = FragmentAdminHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    public void init() {
        BaseQuickAdapter baseQuickAdapter = new BaseQuickAdapter<ItemData, StudentHomeFragment.VH>() {
            @NonNull
            @Override
            protected StudentHomeFragment.VH onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
                StudentHomeFragment.VH vh = new StudentHomeFragment.VH(ItemRecyerviewBinding.inflate(LayoutInflater.from(context), viewGroup, false));
                return vh;
            }

            @Override
            protected void onBindViewHolder(@NonNull StudentHomeFragment.VH vh, int position, @Nullable ItemData itemData) {
                vh.itemViewBidnng.textView.setText(itemData.getName());
                vh.itemViewBidnng.imageView.setImageResource(itemData.getIcon());
                vh.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (vh.getAbsoluteAdapterPosition()) {
                            case 0:
                                startActivity(new Intent(requireActivity(), AnnouncementPushActivity.class));
                                break;
                            case 1:
                                startActivity(new Intent(requireActivity(), ActivityFixHistory.class));
                                break;
                            case 2:
                                startActivity(new Intent(requireActivity(), ActivityDayScore.class));
                                break;
                            case 3:
                                startActivity(new Intent(requireActivity(), ActivityPaymentHistory.class));
                                break;
                            case 4:
                                startActivity(new Intent(requireActivity(), ActivityAdminComeOutHistory.class));
                                break;

                        }

                    }
                });
            }
        };
        QuickAdapterHelper build = new QuickAdapterHelper.Builder(baseQuickAdapter).build();
        mViewBinding.recyclerView.setAdapter(build.getAdapter());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireActivity(), 2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == build.getAdapter().getItemCount() - 1) {
                    return gridLayoutManager.getSpanCount();
                } else {
                    return 1;
                }
            }
        });
        mViewBinding.recyclerView.setLayoutManager(gridLayoutManager);

        ArrayList<ItemData> list = getItemData();
        baseQuickAdapter.submitList(list);
    }

    private static @NonNull ArrayList<ItemData> getItemData() {
        ArrayList<ItemData> list = new ArrayList<>();
        // 公告推送
        ItemData ggts = new ItemData();
        ggts.setIcon(R.mipmap.renwu);
        ggts.setName("公告\n推送");
        list.add(ggts);
        // 保修记录
        ItemData bxjl = new ItemData();
        bxjl.setName("报修\n记录");
        bxjl.setIcon(R.mipmap.ic_repairs);
        list.add(bxjl);

        // 日常评分
        ItemData rcpf = new ItemData();
        rcpf.setIcon(R.mipmap.ic_score);
        rcpf.setName("日常\n评分");
        list.add(rcpf);

        // 缴费审批
        ItemData jfsp = new ItemData();
        jfsp.setName("缴费\n审批");
        jfsp.setIcon(R.mipmap.ic_pay);
        list.add(jfsp);


        // 出入打卡
        ItemData crjl = new ItemData();
        crjl.setName("出入\n记录");
        crjl.setIcon(R.mipmap.ic_come);
        list.add(crjl);
        return list;
    }


    static class VH extends RecyclerView.ViewHolder {
        @NonNull
        public ItemRecyerviewBinding itemViewBidnng;

        public VH(@NonNull ItemRecyerviewBinding itemViewBidnng) {
            super(itemViewBidnng.getRoot());
            this.itemViewBidnng = itemViewBidnng;
        }
    }
}
